import java.util.ArrayList;
import java.util.Scanner;
public class main {
    public static void main(String[] args) {

        //用一个集合存储学生类信息
        ArrayList<Student> stulist=new ArrayList<>();

       loop: while(true)
        {
            System.out.println("--------欢迎使用学生信息管理系统!--------");
            System.out.println("1:添加学生信息");
            System.out.println("2:删除学生信息");
            System.out.println("3:修改学生信息");
            System.out.println("4:查询学生信息");
            System.out.println("5:退出");
            System.out.println("请输入你的选择:");
            Scanner sc=new Scanner(System.in);
            String choice=sc.next();
            switch(choice)
            {
                case "1" : addstu(stulist);
                    break;
                case "2": deletestu(stulist);
                    break;
                case "3":changestu(stulist);
                    break;
                case "4":inquerystu(stulist);
                    break;
                case "5":
                {
                    System.out.println("退出");
                    break loop;
                }

                default:System.out.println("无这个选项请重新输入:");
            }
        }



    }
    //添加信息
    public static void addstu(ArrayList<Student> stulist)
    {
        System.out.println("请输入id");
        Scanner sc=new Scanner(System.in);
        int id;
        while(true) {
             id = sc.nextInt();
            int uid = getuniqueid(id, stulist, sc);
            if (uid != -1)
                System.out.println("id已经存在");
            else break;
        }

        System.out.println("输入姓名");
        String name=sc.next();
        System.out.println("输入年龄");
        int age=sc.nextInt();
        System.out.println("输入号码");
        String number=sc.next();
        //将学生类的信息赋值
       stulist.add(new Student(id,name,age,number));
        //

        System.out.println("已经成功添加");

    }
    //删除学生信息
    public static void deletestu(ArrayList<Student> stulist)
    {
        Scanner sc=new Scanner(System.in);

       System.out.println("确定要删除吗?1:确定   ");
        int ret=sc.nextInt();
        if(ret==1)
        {
            System.out.println("请输入要删除学生的id");
            int id=sc.nextInt();
            for(int i=0;i<stulist.size();i++)
            {
                if(stulist.get(i).getId()==id)
                {
                    stulist.remove(i);
                    System.out.println("已删除");
                    return;
                }
            }
            System.out.println("没有这个id,无法删除");
            return;

        }else return;





    }
    //修改学生信息
    public static void changestu(ArrayList<Student> stulist) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的id");
        int id = sc.nextInt();
        int index = getuniqueid(id, stulist, sc);
        //id存在与否
        if (index != -1) {

            System.out.println("请输入修改后的姓名");
            String newname = sc.next();
            System.out.println("请输入修改后的年龄");
            int newage = sc.nextInt();
            System.out.println("请输入修改后的电话号码");
            String newnumber = sc.next();
            stulist.set(index, new Student(id, newname, newage, newnumber));
        } else
            System.out.println("id不存在无法修改");


    }

    //查询学生信息
    public static void inquerystu(ArrayList<Student> stulist)
    {
        if(stulist.size()!=0)
        {
            System.out.println("id          姓名          年龄          电话号码");
            for(int i=0;i<stulist.size();i++)
            {
                int id=stulist.get(i).getId();
                String name=stulist.get(i).getName();
                int age=stulist.get(i).getAge();
                String number=stulist.get(i).getNumber();

                System.out.print(id+"        ");
                System.out.print(name+"        ");
                System.out.print(age+"         ");
                System.out.println(number);

            }

        }else System.out.println("无学生信息");
        return;

    }
    //判断id是否已经存在(存在返回索引,不存在返回-1)
    public static int getuniqueid(int id,ArrayList<Student> stulist,Scanner sc)
    {
            for(int i=0;i<stulist.size();i++)
                if(stulist.get(i).getId()==id)
                    return i;
           return -1;
        }




}
